import {
  isChina,
  isUrl,
  isJson,
  isPassword,
  validateNull
} from "@/utils/validate";

export function validIdCard(rule: any, value: string, callback: any) {
  const city = {
    11: "北京",
    12: "天津",
    13: "河北",
    14: "山西",
    15: "内蒙古",
    21: "辽宁",
    22: "吉林",
    23: "黑龙江",
    31: "上海",
    32: "江苏",
    33: "浙江",
    34: "安徽",
    35: "福建",
    36: "江西",
    37: "山东",
    41: "河南",
    42: "湖北",
    43: "湖南",
    44: "广东",
    45: "广西",
    46: "海南",
    50: "重庆",
    51: "四川",
    52: "贵州",
    53: "云南",
    54: "西藏",
    61: "陕西",
    62: "甘肃",
    63: "青海",
    64: "宁夏",
    65: "新疆",
    71: "台湾",
    81: "香港",
    82: "澳门",
    91: "国外"
  };
  const cityNo = Number(String(value).substr(0, 2));
  if (!value) {
    return callback();
  }
  if (
    !value ||
    !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(
      value
    )
  ) {
    callback(new Error("身份证号格式错误"));
  } else if (!Object.prototype.hasOwnProperty.call(city, cityNo)) {
    callback(new Error("地址编码错误"));
  } else {
    if (value.length == 18) {
      const valueArray = value.split("");
      const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
      const parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];
      let sum = 0;
      let ai = 0;
      let wi = 0;
      for (let i = 0; i < 17; i++) {
        ai = Number(valueArray[i]);
        wi = factor[i];
        sum += ai * wi;
      }
      if (parity[sum % 11] != valueArray[17]) {
        callback(new Error("校验位错误"));
      } else {
        return callback();
      }
    }
  }
}

export function validRequired(rule: any, value: any, callback: any) {
  if (!value) {
    return callback(new Error("请输入值"));
  } else {
    if (validateNull(value)) {
      return callback(new Error("请输入值"));
    } else {
      return callback();
    }
  }
}

export function validMobilePhone(rule: any, value: any, callback: any) {
  if (value) {
    const reg = /^1[3|4|5|6|7|8][0-9]\d{8}$/;
    if (reg.test(value)) {
      return callback();
    } else {
      return callback(new Error("请输入正确的手机号码"));
    }
  } else {
    return callback();
  }
}

export function validPhone(rule: any, value: any, callback: any) {
  if (!value) {
    return callback();
  } else {
    const reg = /^(\d{3,4}-)?\d{7,8}$/;
    if (reg.test(value)) {
      return callback();
    } else {
      return callback(new Error("请输入正确的电话号码"));
    }
  }
}

export function validEmail(rule: any, value: any, callback: any) {
  if (!value) {
    return callback();
  } else {
    const reg =
      /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
    if (reg.test(value)) {
      return callback();
    } else {
      return callback(new Error("请输入正确的邮箱地址"));
    }
  }
}

export function validEnglish(rule: any, value: any, callback: any) {
  if (!value) {
    return callback();
  } else {
    const reg = /^[a-zA-Z0-9_]*$/;
    if (reg.test(value)) {
      return callback();
    } else {
      return callback(new Error("只能输入字符、数字、下划线"));
    }
  }
}

export function validChinese(rule: any, value: any, callback: any) {
  if (!value) {
    return callback();
  } else {
    if (isChina(value)) {
      return callback();
    } else {
      return callback(new Error("请输入中文"));
    }
  }
}

export function validNumber(rule: any, value: any, callback: any) {
  if (!value) {
    return callback();
  } else {
    const reg = /^-?\d{1,4}(?:\.\d{1,2})?$/;
    if (reg.test(value)) {
      return callback();
    } else {
      return callback(new Error("请输入正确的数字"));
    }
  }
}

export function validTableName(rule: any, value: any, callback: any) {
  if (!value) {
    return callback();
  } else {
    const reg = /^[a-zA-Z0-9_]{1,}$/;
    if (reg.test(value)) {
      return callback();
    } else {
      return callback(new Error("只能以字母开头,允许字母、数字、下划线"));
    }
  }
}

export function validUrl(rule: any, value: any, callback: any) {
  if (!value) {
    return callback();
  } else {
    if (isUrl(value)) {
      return callback();
    } else {
      return callback(new Error("请输入合法的网址"));
    }
  }
}

export function validJson(rule: any, value: any, callback: any): any {
  if (!value) {
    return callback();
  } else {
    console.log("validJson", value, isJson(value));
    if (isJson(value)) {
      return callback();
    } else {
      return callback(new Error("请输入合法的JSON字符串"));
    }
  }
}

export function validPassword(rule: any, value: any, callback: any): any {
  if (!value) {
    return callback();
  } else {
    console.log("validPassword", value, isPassword(value));
    if (isPassword(value)) {
      return callback();
    } else {
      return callback(
        new Error("密码8~20位，且必须包含大写字母、小写字母、数字及特殊符号")
      );
    }
  }
}
